﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Web.UI;

namespace ChoViet.Lib.Helpers
{
	public static class DropDownListHelper
	{
		public static string DropDownList(this HtmlHelper helper, string name, string lableOption, int minValue, int maxValue, int selectedValue)
		{
			return DropDownList(helper, name, lableOption, minValue, maxValue, selectedValue, null);
		}
		public static string DropDownList(this HtmlHelper helper, string name, string lableOption, int minValue, int maxValue, int selectedValue, IDictionary<string, object> htmlAttributes)
		{
			StringBuilder htmlBuilder = new StringBuilder();
			using (TextWriter stringWriter = new StringWriter(htmlBuilder))
			{
				using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Name, name);
					writer.AddAttribute(HtmlTextWriterAttribute.Id, name);
					if (htmlAttributes != null)
					{
						foreach (var k in htmlAttributes.Keys)
						{
							writer.AddAttribute(k.ToString(), htmlAttributes[k].ToString());
						}
					}
					writer.RenderBeginTag(HtmlTextWriterTag.Select);

					writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
					writer.RenderBeginTag(HtmlTextWriterTag.Option);
					writer.Write(lableOption);
					writer.RenderEndTag();

					for (int i = minValue; i <= maxValue; i++)
					{
						writer.AddAttribute(HtmlTextWriterAttribute.Value, i.ToString());

						if (i == selectedValue)
						{
							writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
						}
						writer.RenderBeginTag(HtmlTextWriterTag.Option);
						writer.Write(i.ToString());
						writer.RenderEndTag();
					}
					writer.RenderEndTag();
				}
			}
			return htmlBuilder.ToString();
		}
	}
}